package org.yourscrum.persistence;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

/**
 * Parent class for all JDO Repository implementations
 *
 * @author Jettro Coenradie
 */
public class JdoRepository {
    private PersistenceManagerFactory pmFactory;

    public <T> T storeEntity(T t) {
        PersistenceManager pm = pmFactory.getPersistenceManager();

        try {
            T o = pm.makePersistent(t);
            return pm.detachCopy(o);
        } finally {
            pm.close();
        }
    }

    /*default*/ PersistenceManagerFactory getPmFactory() {
        return pmFactory;
    }

    @Autowired()
    public void setPersistenceManagerFactory(@Qualifier("persistenceManagerFactory")PersistenceManagerFactory persistenceManagerFactory) {
        pmFactory = persistenceManagerFactory;
    }

}
